<?php

namespace App\Repositories\GameTool;


use App\Models\GameTool\ChatLog;

class ChatLogRepository
{
    public function getAllChatLog($request)
    {
        $query = ChatLog::query();
        $gameID = session('game_id');

        $query->where('op_time', '>=', $request['start_time'] . ' 00:00:00');
        $query->where('op_time', '<=', $request['end_time'] . ' 23:59:59');
        $query->where('game_id', '=', $gameID);

        if ($request->has('uid') && !empty($request['uid'])) {
            $query->where('uid', '=', $request['uid']);
        }
        if ($request->has('object_id') && !empty($request['object_id'])) {
            $query->where('_id', '>', new \MongoDB\BSON\ObjectId($request['object_id']));
        }

        if ($request->has('unique_id')) {
            $query->whereIn('unique_id', $request['unique_id']);
        }
        if ($request->has('chat_channel_id')) {
            $query->whereIn('chat_channel_id', $request['chat_channel_id']);
        }

        if ($request->has('nickname') && !empty($request['nickname'])) {
            $query->where('nickname', 'like', '%' . $request['nickname'] . '%');
        }
        if ($request->has('content') && !empty($request['content'])) {
            $query->where('content', 'like', '%' . $request['content'] . '%');
        }

        return $query->latest('op_time')->paginate($request['page_size']);
    }

    public function getByID($id)
    {
        return ChatLog::find($id);
    }

    public function update(array $data, $id)
    {
        return ChatLog::query()->where('id', $id)->update($data);
    }

    /**
     * 获取全部的 聊天类型
     * @return mixed
     */
    public function getChatChannelTypes()
    {
        $gameID = session('game_id');

        $query = [
            [
                '$match' =>
                    [
                        'game_id' =>
                            [
                                '$eq' => $gameID
                            ],
                    ]
            ],
            [
                '$group' =>
                    [
                        '_id' =>
                            [
                                'chat_channel_id' => '$chat_channel_id',
                            ],
                        'chat_channel_name' =>
                            [
                                '$last' => '$chat_channel_name'
                            ],
                    ]
            ],
            [
                '$sort' =>
                    [
                        '_id.chat_channel_id' => 1
                    ]
            ]
        ];

        $collection = ChatLog::raw(function ($collection) use ($query) {
            return $collection->aggregate($query);
        });

        $data = $collection->toArray();
        $formatData = [];
        foreach ($data as $key => $value) {
            $formatData[] = [
                'chat_channel_id' => $value['_id']['chat_channel_id'],
                'chat_channel_name' => $value['chat_channel_name'],
            ];
        }

        return $formatData;
    }
}